import { Component, OnInit, Input, Inject, forwardRef } from '@angular/core';

// import { TabsService } from 'src/app/shared/service/tabs.service';

import { AppGlobalService } from '../../shared/service/app-global.service';
import { EventService } from '../../shared/service/event.service';
import { HttpService } from '../../shared/service/http.service';
import { AlertController } from '@ionic/angular';
import { config } from 'rxjs';
import { ActionSheetService } from "ng-zorro-antd-mobile";

@Component({
  selector: 'app-presales-information',
  templateUrl: './presales-information.component.html',
  styleUrls: ['./presales-information.component.scss'],
})
export class PresalesInformationComponent implements OnInit {

  constructor(
    private alert: AlertController,
    private http: HttpService,
    private appGlobalService: AppGlobalService,
    private eventService: EventService,
  ) { }
  @Input() tagData: any;
  isshowhistory = false;   // 判断展示还是隐藏
  sessionId: string;
  receiveList: any;
  isDataShow = false;
  ifget: any;
  foldimg: any;
  foldimgId = 'foldimgId';
  ngOnInit() {
    this.foldimgId = this.foldimgId + new Date().getTime();
    /*  this.tagData.data={
         "button":{
             "visible":true,
             "submit":{
                 "id":"sureReceive",
                 "content":{
                     "opportunity_opportunity_status":"0",
                     "creator_info":"深圳李xx电话",
                     "opportunity_opportunity_id":"5cff255ceeffaa2eac5570ca",
                     "eventPosition":"before"
                 }
             },
             "label":"领取",
             "type":"service"
         },
         "expand":[
             {
                 "style":"1",
                 "content":"ces是2019-06-12 18:19:08发布"
             },
             {
               "style":"1",
               "content":"2019-06-12 18:19:08发布"
           },{
             "style":"1",
             "content":"2019-06-12 18:19:08发布"
         }
         ],
         "display":{
             "style":"1",
             "content":"待领取11"
         }
     };  */
    this.sessionId = localStorage.getItem('sessionId');
    this.foldimg = "../../../assets/img/folddown.png";
    if (this.tagData.data.display) {
      this.ifget = true;
    } else {
      this.ifget = false;
    }
    if (this.isDataShow === true) {
      this.foldimg = "../../../assets/img/foldup.png";
    } else {
      this.foldimg = "../../../assets/img/folddown.png";
    }
  }
  operationclick() {
    const content = this.tagData.data.button.submit.content;
    const listId = this.tagData.data.button.submit.id;
    const params = {
      content,
      id: listId,
      sessionId: this.sessionId
    };
    if (this.tagData.data.button.type === "service") {
      this.cancel();
    } else {
      // 请求获取新数据
      this.http.post('application/submit', params, res => {
        // 请求数据成功
        if (res.success === true) {
          this.tagData.data.expand = res.data.expand;
          this.ifget = false;
          this.appGlobalService.showToast(res.message, 2000, 'middle');
          this.appGlobalService.reloadCurrentPage();
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
          /* localStorage.setItem('needRefresh', 'true'); */
        }
      });
    }
  }
  showclick() {
    this.isshowhistory = !this.isshowhistory;

    if (this.isshowhistory === true) {
      this.foldimg = "../../../assets/img/foldup.png";
    } else {
      this.foldimg = "../../../assets/img/folddown.png";
    }


    // console.log(this.isshowhistory);
  }
  onReceiveChange(index) {
    if (index === 0) {
      this.isDataShow = !this.isDataShow;
      if (this.isDataShow === true) {

        //
        if(this.tagData.data.display){

        }
        this.foldimg = "../../../assets/img/foldup.png";
      } else {
        this.foldimg = "../../../assets/img/folddown.png";
      }
    }
  }
  async cancel() {
    const content = this.tagData.data.button.submit.content;
    const listId = this.tagData.data.button.submit.id;
    const params = {
      content,
      id: listId,
      sessionId: this.sessionId
    };
    const tip = "领取后不能自行放弃，确定要领取服务吗？";
    const alert = await this.alert.create({
      message: tip,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
          }
        }, {
          text: '确定',
          cssClass: 'LJ',
          handler: () => {
            this.http.post('application/submit', params, res => {
              // 请求数据成功
              if (res.success === true) {
                this.tagData.data.expand = res.data.expand;
                this.ifget = false;
                this.appGlobalService.showToast(res.message, 2000, 'middle');
                this.appGlobalService.reloadCurrentPage();
              } else {
                // 请求数据失败
                this.appGlobalService.showToast(res.message, 2000, 'middle');
                /* localStorage.setItem('needRefresh', 'true'); */
              }
            });
          }
        }
      ]
    });
    await alert.present();
  }

  showActionSheet = (item) => {
    const buttons = item.formData.map(
      obj => obj.label
    );
    ActionSheetService.showActionSheetWithOptions(
      {
        options: buttons
      },
      buttonIndex => {
        console.log(buttonIndex);
        if (buttonIndex >= 0) {
          this.go(
            item.formData[buttonIndex]
          );
        }
      }
    );
  }
  /**
   * 跳转备注页面
   *
   * @memberof DetailComponent
   */
  go(item) {
    if (item.link && item.link.type === "url") {

      this.appGlobalService.openUrl('', item.link.url);
    } else if (item.link && item.link.type === "page") {
      // page
      this.appGlobalService.openPage(item.link);
    } else if (item.submit) {
      const params = {
        content: item.submit.content,
        id: item.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.http.post("application/submit", params, async res => {
        if (res.success === true) {
          this.appGlobalService.showToast(res.message, 2000, 'middle');
          await this.http.reloadCurrentPage();
          // await localStorage.setItem("needRefresh", "true");
        } else {
          this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
      });
    }
  }


  /*//备份
  operationclick() {
      let content = this.tagData.data.button.submit.content;
      let listId = this.tagData.data.button.submit.id;
      const params = {
        content,
        id: listId,
        sessionId: this.sessionId
      };
      // 请求获取新数据
      this.http.post('application/submit', params, res => {
        // 请求数据成功
        if ( res['success'] === true) {
          this.tagData.data.expand = res.data.expand;
          this.ifget = false;
          this.appGlobalService.showToast(res['message'], 2000, 'middle');
          this.tabsService.reloadCurrentPage();
          setTimeout(() => {
            localStorage.setItem('needRefresh','true');
          }, 2000);
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(res['message'], 2000, 'middle');
          localStorage.setItem('needRefresh','true');
        }
      });
    }
   */
}
